package com.example.youdu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aostarit.sgid.agent.EncryptHelper;
import com.aostarit.smcrypto.exception.InvalidKeyException;
import com.aostarit.smcrypto.exception.InvalidSourceDataException;
import com.example.youdu.dto.CallBackDto;
import com.example.youdu.service.IAccessTokenService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/********************************************************************************
 ** author： zhangtianhe
 ** date：2024年07月23日 15:35
 ** description&desc：
 *********************************************************************************/
@Service
public class AccessTokenServiceImpl implements IAccessTokenService {
    @Value("${nm.appid}")
    private String appId;//appId
    @Value("${nm.client_secret}")
    private String client_secret;//客户端秘钥
    @Value("${nm.privateKey}")
    private String privateKey;//加密key
    @Value("${nm.publicKey}")
    private String publicKey;//签名key
    @Value("${nm.jkurl}")
    private String jkurl;//接口请求地址

    @Override
    public String  getAccessToken() throws Exception{
        String url = jkurl+"/sgid-base-control-app/res/iscMincroService/getAccessToken" ;
        JSONObject jsonObject =new JSONObject();
        jsonObject.put("appId",appId);
        jsonObject.put("clientSecret",client_secret);
        Map header = new HashMap();
        String param =jsonObject.toJSONString();
        String signData = null;
        signData = EncryptHelper.sign(publicKey,param);
        header.put("X-Acloud-Data-sign",signData);
        header.put("X-Clientid", appId);
        System.out.println("----getAccessToken请求数据---"+jsonObject.toJSONString());
        String data = cn.hutool.http.HttpUtil.createPost(url).addHeaders(header).body(jsonObject.toJSONString()).execute().body();
        JSONObject result =JSONObject.parseObject(data);
        System.out.println("----getAccessToken返回结果---"+result);
        return result.getJSONObject("data").getString("accessToken");
    }

    @Override
    public JSONObject login(CallBackDto callBackDto) throws Exception{

        String url = jkurl+"/sgid-base-identity-authentication/identityAuthorize/loginByPWD" ;
        JSONObject jsonObject =new JSONObject();
        jsonObject.put("loginName",callBackDto.getFromUser());
        String pwd =callBackDto.getPasswd();
        jsonObject.put("pwd",EncryptHelper.encryptPWD(privateKey,pwd));
        Map header = new HashMap();
        String param =jsonObject.toJSONString();
        String signData = null;
        try {
            signData = EncryptHelper.sign(publicKey,param);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (InvalidSourceDataException e) {
            throw new RuntimeException(e);
        }
        String token = this.getAccessToken();
        header.put("X-ISC-AccessToken",token);
        header.put("X-Acloud-Data-sign",signData);
        header.put("X-Clientid", appId);
        System.out.println("----login请求数据---"+param);
        String data = cn.hutool.http.HttpUtil.createPost(url).addHeaders(header).body(param).execute().body();
        JSONObject result =JSONObject.parseObject(data);
        System.out.println("----login返回数据---"+data);
        return result;
    }

}
